/*
 * Created by SharpDevelop.
 * User: fatman
 * Date: 10/27/2008
 * Time: 12:20 PM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Collections.Generic;

namespace CUALib
{
	/// <summary>
	/// Description of Util.
	/// </summary>
	public class Util
	{
		public static List<String> Wrap0(string text, int maxLength)
		{

			// Return empty list of strings if the text was empty
			if (text.Length == 0) return new List<string>();

			string[] words = text.Split(new char[]{' ','\n'});
			List<string> lines = new List<string>();
			string currentLine = "";

			foreach (string currentWord in words)
			{

				if ((currentLine.Length > maxLength) || ((currentLine.Length + currentWord.Length) > maxLength))
				{
					lines.Add(currentLine);
					currentLine = "";
				}

				if (currentLine.Length > 0)
					currentLine += " " + currentWord;
				else
					currentLine += currentWord;

			}

			if (currentLine.Length > 0)
				lines.Add(currentLine);

			
			return lines;
		}

		public static List<String> SplitWord(string text, int maxLength)
		{
			List<string> ret=new List<string>();
			for(int i=0;i<text.Length/maxLength;i++)
			{
				ret.Add(text.Substring(maxLength*i,maxLength));
			}
			string rest=text.Substring(maxLength*(int)(text.Length/maxLength));
			if(rest.Length>0)
			{
				ret.Add(rest);
			}
			return ret;
		}
		public static List<String> Wrap(string text, int maxLength)
		{

			// Return empty list of strings if the text was empty
			if (text.Length == 0) return new List<string>();

			string[] words = text.Split(new char[]{' ','\n'});
			List<string> lines = new List<string>();
			string currentLine = "";

			
			
			foreach (string currentWord in words)
			{

				string _currentWord=currentWord;
				if(_currentWord.Length==0)
					continue;
				
				//Console.WriteLine("-> {0}",_currentWord);
				if(currentWord.Length>maxLength)
				{
					if(currentLine.Length>0)
						lines.Add(currentLine);
					
					//Console.WriteLine("-- {0}",_currentWord);
					List<string> subWords=Util.SplitWord(currentWord,maxLength);
					for(int swi =0; swi<subWords.Count-1;swi++)
					{
						lines.Add(subWords[swi]);
					}
					
					_currentWord = subWords[subWords.Count-1];
					//currentLine = _currentWord;
					currentLine = "";
					
				}
				
				
				
				if ((currentLine.Length + _currentWord.Length) > maxLength && currentLine.Length>0)
				{
					lines.Add(currentLine);
					currentLine = "";
				}

				if (currentLine.Length > 0)
					currentLine += " " + _currentWord;
				else
					currentLine += _currentWord;

				//Console.WriteLine("== {0}",_currentWord);
				
			}

			if (currentLine.Length > 0)
				lines.Add(currentLine);

			
			return lines;
		}

	}
}
